// pages/goodsDetails/goodsDetails.js
const API = require('../../API/api.js');
const app = getApp()
Page({
    goIndex() {
        wx.redirectTo({
            url: '../home/home',
        })
    },
    buy() {
        let goodsId = this.data.goodsId;
        let skuId = this.data.index;
        wx.navigateTo({
            url: '../orderInfo/orderInfo?goodsId=' + goodsId + '&skuId=' + skuId,
        })
    },
    /**
     * 页面的初始数据
     */
    data: {
        imgUrls: [
            'https://kximg.kuanxingjiuye.com/img/banner/banner1.png',
            'https://kximg.kuanxingjiuye.com/img/banner/banner2.png'
        ],
        indicatorDots: false,
        autoplay: false,
        interval: 5000,
        duration: 1000,
        imgUrl: app.globalData.imgUrl,
        goodsId: null,
        integer: '',
        decimals: '',
        level: null,
        discounts: null,
        nodes: '',
        goodsDetails: [],
        name: '',
        limitBuy: null,
        skuName: [],
        index: 1
    },
    onSku(e) {
        console.log(e);
        this.setData({
            index: e.currentTarget.dataset.sku
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //请求商品详情
        console.log(options.goodsId);
        API.getGoodsDetails({
            id: options.goodsId,
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            console.log(res)
            this.setData({
                goodsId: options.goodsId,
                integer: res.data.list[0].salesPrice,
                nodes: res.data.infoDetail,
                name: res.data.name,
                limitBuy: res.data.list[0].limitBuy,
                skuName: res.data.list,
                index: res.data.list[0].id
            })
        })
        API.getMyUserInfo({
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            console.log(res)
            this.setData({
                level: res.data.level
            })
        })
        //判断价格是否整数
        //let isInteger = Number.isInteger(this.data.price);
        let price;
        // if(!isInteger) {
        //   price = this.data.price.toFixed(2).split(".");
        //   this.setData({
        //     integer:price[0],
        //     decimals:price[1]
        //   })
        // }else{
        //   this.setData({
        //     integer:this.data.price
        //   })
        // }
        //根据等级判断优惠额度
        if (this.data.level === '酒友') {
            this.setData({
                discounts: 18
            })
        } else if (this.data.level === '掌柜') {
            this.setData({
                discounts: 28
            })
        } else if (this.data.level === '堂主') {
            this.setData({
                discounts: 36
            })
        } else if (this.data.level === '联创') {
            this.setData({
                discounts: 42
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
